<?php

class Joob_Session_Standard {
	
	private static $instance;
	
	protected $behaviour;
	
	protected $_isDestroyed;
	
	protected $_isStarted;
	
	protected $_id;
	
	public static function getInstance()	{
		if (self::$instance == null)	{
			self::$instance = new Joob_Session_Standard();
		}
		return self::$instance;
	}

	/**
	 * 
	 * @return the $behaviour
	 */
	public function getBehaviour() {
		return $this->behaviour;
	}

	/**
	 * @param field_type $behaviour
	 */
	public function setBehaviour($behaviour) {
		$this->behaviour = $behaviour;
	}

	private function __construct()	{
		
	}
	
	public function set($key, $value)	{
		return $this->behaviour->set($key, $value);
	}
	
	public function get($key)	{
		return $this->behaviour->get($key);
	}
	
	/**
	 * Start the session
	 * @throws Exception if headers is already sent
	 */
	public function start()	{
		if ($this->_isStarted)	{
			return true;
		}
		
		if (headers_sent())	{
			throw new Exception("Cannot start session after output is sent", 500);
		}
		
		if (session_start())	{
			$this->_isStarted = true;
			$this->_id = session_id();
			return true;
		}
		
		return false;
	}
	
	/**
	 * Destroy the session
	 */
	public function destroy()	{
		if ($this->_isDestroyed)	{
			return true;
		}
		
		if (session_destroy())	{
			$this->_isDestroyed = true;
			$this->_id = null;
			return true;
		}
		
		return false;
	}
	
	public function isDestroyed()	{
		return $this->_isDestroyed;
	}
}

?>